define(function () {
    'use strict';

    function ctrl($scope,$ionicPopup,$q,$timeout,$ionicScrollDelegate,$stateParams,$filter,
                  LocalCacheService,InfoService,HistoryNavChartService,WebService,$ionicLoading, CommonService) {

        $scope.channel = {};
        $scope.vm = {};
        $scope.vm.activeTab=1;
        $scope.csi300ChartConfig = HistoryNavChartService.historyIndexChart();

        $scope.$on('$ionicView.beforeEnter', function() {
            init();
        });

        $scope.doRefresh = function() {
            init().finally(function(){
                $scope.$broadcast('scroll.refreshComplete');
            })
        };

        $scope.$watch("vm.activeTab", function(newValue, oldValue){
            if(newValue) {
                $timeout(function(){
                    $ionicScrollDelegate.resize();
                }, 500);
            }
        });

        function init(){
            if(!$scope.ads){
                $ionicLoading.show();
            }
            $scope.id = $stateParams['id'];
            $scope.showAds();
            return $q.all([queryIndexProfit(), queryCSI300Index(), queryContentBriefs()])
        }

        $scope.showAds = function(type){
            WebService.wtAd("xcm_indexTeam_1").then(function(data){
                if(data) {
                    $scope.ads = data;
                    var adKey = "ad_" + $scope.channel.adId;
                    if((!LocalCacheService.get(adKey) || type) && $scope.ads.length > 0) {
                        var myPopup = $ionicPopup.show({
                            cssClass: "kDialog",
                            templateUrl: "product/ads.html",
                            scope: $scope,
                            buttons: [
                                { text: '确定' }
                            ]
                        });
                        LocalCacheService.set(adKey, true);
                    }
                }
            });
        }

        //查询指数收益率
        function queryIndexProfit(){
            var p = InfoService.getIndexProfit($scope.channel.title);
            p.then(function(data){
                if(data){
                    $scope.index = data[0];
                }
            });
            return p;
        }

        //绘制沪深300走势图
        function queryCSI300Index(){
            var p = InfoService.getHistoryIndex($scope.channel.title,60);
            p.then(function(data){
                if(data){
                    //画收盘走势图
                    $scope.csi300ChartConfig = HistoryNavChartService.historyIndexChart(data);
                    $scope.data_date = CommonService.formatDate(data[data.length - 1].data_date);
                }
            });
            return p;
        }

        //获取营地基金概况
        function queryContentBriefs(){
            return InfoService.getChannelInfosByParentId($scope.id).then(function(data){
                $scope.channels = data;
                for(var i = 0; i < $scope.channels.length; i++) {
                    var channel = $scope.channels[i];
                    if(i == 0) {
                        $scope.vm.activeTab=channel.id;
                    }
                    (function(c){
                        InfoService.getProductsByChannelId(c.id).then(
                            function(funds) {
                                c.funds =  funds;
                                for(var i=0;i<c.funds.length;i++){
                                    var fund = c.funds[i];
                                    fund.prod_min_subscribe = $filter('prodMinSubscribe')(CommonService.getMinAmount(fund));
                                }
                            }
                        )
                    })(channel);
                    $ionicLoading.hide();
                }
            });
        }

    }

    ctrl.$inject = ['$scope','$ionicPopup','$q','$timeout','$ionicScrollDelegate','$stateParams','$filter',
        'LocalCacheService','InfoService','HistoryNavChartService','WebService','$ionicLoading', 'CommonService'];
    return ctrl;
});